package com.freemyleft.left.zapp.delegates.Bottom;

import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.ColorInt;
import android.support.annotation.Nullable;
import android.support.v7.widget.AppCompatTextView;
import android.support.v7.widget.LinearLayoutCompat;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.RelativeLayout;

import com.freemyleft.left.R;
import com.freemyleft.left.R2;
import com.freemyleft.left.zapp.delegates.LeftDelegate;
import com.joanzapata.iconify.widget.IconTextView;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.Map;

import butterknife.BindView;
import me.yokeyword.fragmentation.ISupportFragment;

/**
 * Created by FreeMyleft on 2017/12/24.
 */

/**
 * 这样一个类所需的几个东西 第一个实体类
 */
public abstract class BaseBottomDelegate extends LeftDelegate implements View.OnClickListener {
    @BindView(R2.id.bottom_bar)
    LinearLayoutCompat mBottomBar = null;
    //用来存储所有的子Frgement
    private final ArrayList<BottomItemDelegate> ITEM_DELEGATE = new ArrayList<>();
    //用来存储所有的子bean
    private final ArrayList<BottomTabBean> TAB_BENS = new ArrayList<>();

    private final LinkedHashMap<BottomTabBean, BottomItemDelegate> ITEMS = new LinkedHashMap<>();
    //记录当前的Frgement是那个Frgement
    private int mCrrentDelegate = 0;
    //第一个展示的Frgement
    private int mIndexDelegate = 0;
    private int mClickedColor = Color.BLUE;//点击时的颜色


    public abstract LinkedHashMap<BottomTabBean, BottomItemDelegate> setItems(ItemBuilder builder);

    @Override
    public Object setLayout() {
        return R.layout.delegate_bottom;
    }

    public abstract int setIndexDelegate();

    //告诉编译器这必须是一个颜色
    @ColorInt
    public abstract int setClickedColor();

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mIndexDelegate = setIndexDelegate();
        if (setClickedColor() != 0) {
            mClickedColor = setClickedColor();
        }
        final ItemBuilder builder = ItemBuilder.buider();
        final LinkedHashMap<BottomTabBean, BottomItemDelegate> Items = setItems(builder);
        ITEMS.putAll(Items);
        for (Map.Entry<BottomTabBean, BottomItemDelegate> item : ITEMS.entrySet()) {
            final BottomTabBean key = item.getKey();
            final BottomItemDelegate value = item.getValue();
            TAB_BENS.add(key);
            ITEM_DELEGATE.add(value);

        }
    }

    @Override
    public void onBindView(@Nullable Bundle savedInstanceState, View rootView) {

        final int size = ITEMS.size();
        for (int i = 0; i < size; i++) {
            LayoutInflater.from(getContext()).inflate(R.layout.bottom_item_icon_text_layout, mBottomBar);
            final RelativeLayout item = (RelativeLayout) mBottomBar.getChildAt(i);
            //设置每一个Item的点击事件
            item.setTag(i);
            item.setOnClickListener(this);
            final IconTextView itemIcon = (IconTextView) item.getChildAt(0);
            final AppCompatTextView itemTiele = (AppCompatTextView) item.getChildAt(1);
            final BottomTabBean bean = TAB_BENS.get(i);
            //初始化数据
            itemIcon.setText(bean.getIcon());
            itemTiele.setText(bean.getTitle());
            if (i == mIndexDelegate) {
                itemIcon.setTextColor(mClickedColor);
                itemTiele.setTextColor(mClickedColor);
            }

        }
        final ISupportFragment[] delegateArray = ITEM_DELEGATE.toArray(new ISupportFragment[size]);
        getSupportDelegate().
                loadMultipleRootFragment
                        (R.id.bottom_bar_delegate_container, mIndexDelegate, delegateArray);

    }

    private void resetColor() {
        final int count = mBottomBar.getChildCount();
        for (int i=0; i < count; i++) {
            final RelativeLayout item = (RelativeLayout) mBottomBar.getChildAt(i);
            final IconTextView itemIcon = (IconTextView) item.getChildAt(0);
            itemIcon.setTextColor(Color.GRAY);
            final AppCompatTextView itemTiele = (AppCompatTextView) item.getChildAt(1);
            itemTiele.setTextColor(Color.GRAY);
        }

    }

    @Override
    public void onClick(View view) {
        final int tag= (int) view.getTag();
        resetColor();
        final RelativeLayout item= (RelativeLayout) view;
        final IconTextView itemIcon = (IconTextView) item.getChildAt(0);
        itemIcon.setTextColor(mClickedColor);
        final AppCompatTextView itemTiele = (AppCompatTextView) item.getChildAt(1);
        itemTiele.setTextColor(mClickedColor );
        getSupportDelegate().showHideFragment(ITEM_DELEGATE.get(tag),ITEM_DELEGATE.get( mCrrentDelegate));
        //注意先后顺序
        mCrrentDelegate=tag;

    }
}
